<?php
namespace app\common\service;

use common\util\ArrayUtil;

class MemberService {
    
    public static function getSubList($pid){
        
        $memberModel = \app\common\model\MemberModel::instance();
        $items = $memberModel->where('fid',$pid)->select();
        $items = collection($items)->toArray();
        $list = [];
        if($items){
            $mids = ArrayUtil::getKeyValuesFromArray($items, 'id');
            $res = $memberModel->whereIn('fid',$mids)->group('fid')->column('count(0)','fid');
            
            foreach ($items as $item){
                $id = $item['id'];
                $subCount = array_key_exists($id, $res) ? $res[$id] : 0;
                $fenxiaoCount =  $memberModel->where("FIND_IN_SET($id, pid_path)")->cache(180)->count();
                $list [] = [
                    'id'=>$id,
                    'avatar'=> get_image($item['face']),
                    'name'=> $item['name'],
                    'direct_count'=> $subCount,//直属
                    'sub_count'=> ($fenxiaoCount - $subCount),//分销总数 - 直属数量
                ];
            }
        }
        return $list;
    }
}